**** Script for analyzing speed bins data
* October 2019

*display options
set scheme simple // setting scheme for graphs - personal one

* ssc install parmest
* need version of parmest that works with 14.1
* net install "http://www.rogernewsonresources.org.uk/stata11/parmest" , replace

* location of datafiles
*local datafile = "${base_dir}Stata/CA_tracks/CA_speed_bins.csv" // has fuel etc
*local datafile = "${base_dir}Stata/CA_tracks/CA_speed_bins_04082020.csv" //
local datafile = "${base_dir}/data/final/CA_speed_bins.csv" //
local analysis_dta = "${base_dir}/data/final/speed_bins.dta"
local reassemble = 1

* location of fuel price and cpi data
local price_file "${base_dir}/data_not_to_share/SPfuelprices/SPfuelprices.csv"
local cpi_file "${base_dir}/data/CPI/CPIAUCSL"
local unem_file "${base_dir}/data/unemployment/UNRATE"


* Assembling *******************************************************************
if `reassemble'==1 {

	disp "`unem_file'"

	* make WFR data
	*do "${base_dir}Stata/CA_tracks/load_vessel_char.do" `wfr_folder' `wfr_date' `wfrdta'
	*do "${base_dir}Stata/CA_tracks/load_vessel_list.do" `vesselList' `vesselList_dta'
	
	do "${base_dir}/analysis_code/speed_bins_assemble" ///
		"`datafile'" "`price_file'" "`cpi_file'" "`unem_file'"
	save "`analysis_dta'" , replace
}
else {
	use "`analysis_dta'" , replace
}

* INITIAL CLEANING *************************************************************
drop if date1<mdy(1,1,2009) 

drop if route_name=="LALB_LALB" 
										
replace length=. if length>450 // clearing really long boats (probably data error)

drop if inlist(group_agg,"Passenger","Miscel")
drop if inlist(group,"Tug","Dredge")

* dropping PW Sound and Cook Inlet routes
drop if otherAK == 1 

* dropping HI to HI routes
drop if HItoHI == 1 

* if track crosses land too much then drop
drop if km_onland>5

* drop if fuel consumption equals zero
*drop if f_cons==0 | f_power==0

**** other cleaning
do "${base_dir}/analysis_code/cleaning.do"

********************************************************************************
* recovering eca dates
levelsof eca_ind if eca_ind>0 , local(eca_inds)
foreach i of local eca_inds {
	qui sum date1 if eca_ind==`i'
	scalar eca`i'd = r(min)
	scalar eca`i'qy = yq( year(r(min)),quarter(r(min)) ) 
	scalar eca`i'my = ym( year(r(min)),month(r(min)) ) 
}

* renaming distance bins
rename d_km dist_b
rename d_km_eca09 dist_b_eca09
rename d_km_eca11 dist_b_eca11

gen dist_b_out09 = dist_b - dist_b_eca09 
gen dist_b_out11 = dist_b - dist_b_eca11 

* summing across speed bins
egen sum_dist_b = sum(dist_b) , by(track_id)


gen dist_diff = dist - sum_dist_b

gen dist_b_shr = dist_b / sum_dist_b

egen kmh_b4 = cut(kmh_b) , at(0,8,16,20,24,28,32,36,40,44,48)
egen kmh_b8 = cut(kmh_b) , at(0,20,28,36,44,48)

gen zero_b = (dist_b==0)

gen t_eca09 = date1 - eca1d
gen t_eca11 = date1 - eca2d
gen t_eca12 = date1 - eca3d
*gen t_eca15 = date1 - eca5d 

***** Create Bin Plots - welfare paper
capture mkdir "${base_dir}/results/rd_plots"
do "${base_dir}/analysis_code/bin_plots"
